package com.fishleap.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author zlg
 */
public class WordcountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private Text outKey = new Text();
    private IntWritable outValue = new IntWritable(1);

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 1.将一行文本转为字符串
        String line = value.toString();
        // 2.切割
        String[] words = line.split(" ");
        // 3.输出
        for (String word : words) {
            outKey.set(word);
            context.write(outKey, outValue);
        }
    }
}
